package com.exchange.repository;

import com.exchange.entity.ExchangeCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface ExchangeCodeRepository extends JpaRepository<ExchangeCode, Long> {
    Optional<ExchangeCode> findByCode(String code);
    List<ExchangeCode> findByMerchantId(Long merchantId);
    List<ExchangeCode> findByMerchantIdAndStatus(Long merchantId, ExchangeCode.ExchangeCodeStatus status);
    
    @Query("SELECT COUNT(ec) FROM ExchangeCode ec WHERE ec.merchantId = :merchantId")
    Long countByMerchantId(@Param("merchantId") Long merchantId);
    
    @Query("SELECT COUNT(ec) FROM ExchangeCode ec WHERE ec.merchantId = :merchantId AND ec.createdAt >= :startOfMonth")
    Long countByMerchantIdAndMonth(@Param("merchantId") Long merchantId, 
                                   @Param("startOfMonth") LocalDateTime startOfMonth);
}